# -*- coding: utf-8 -*-
import pandas as pd
import talib as ta
import numpy as np
import matplotlib.pyplot as plt
from fbprophet import Prophet
from py_mysql import *
from tqdm import tqdm

csv_arr = []
def run_all(code,startDate,endDate):
    print('start----',code)
    obj_data = {
        'code': code,
        'score': None,
        'start_date': startDate,
        'end_date': endDate,
        'y_pred_proba': None,
        'importances_sort_df': None
    }
    startDate = startDate #开始时间
    endDate = endDate   #结束时间
    codeArr = [code]   #品种数组
    query_db = Mysql_search()
    df = query_db.get_one(codeArr,startDate,endDate)
    df = df[codeArr[0]]

    # 形态识别
    # CDL2CROWS - Two Crows
    df['CDL2CROWS'] = ta.CDL2CROWS(df['open'], df['high'],df['low'],df['close'])
    # CDL3BLACKCROWS - Three Black Crows
    df['CDL3BLACKCROWS'] = ta.CDL3BLACKCROWS(df['open'], df['high'],df['low'],df['close'])
    # CDL3INSIDE - Three Inside Up/Down
    df['CDL3INSIDE'] = ta.CDL3INSIDE(df['open'], df['high'],df['low'],df['close'])
    # CDL3LINESTRIKE - Three-Line Strike
    df['CDL3LINESTRIKE'] = ta.CDL3LINESTRIKE(df['open'], df['high'],df['low'],df['close'])
    # CDL3OUTSIDE - Three Outside Up/Down
    df['CDL3OUTSIDE'] = ta.CDL3OUTSIDE(df['open'], df['high'],df['low'],df['close'])
    # CDL3STARSINSOUTH - Three Stars In The South
    df['CDL3STARSINSOUTH'] = ta.CDL3STARSINSOUTH(df['open'], df['high'],df['low'],df['close'])
    # CDL3WHITESOLDIERS - Three Advancing White Soldiers
    df['CDL3WHITESOLDIERS'] = ta.CDL3WHITESOLDIERS(df['open'], df['high'],df['low'],df['close'])
    #CDLABANDONEDBABY - Abandoned Baby
    df['CDLABANDONEDBABY'] = ta.CDLABANDONEDBABY(df['open'], df['high'],df['low'],df['close'], penetration=0)
    #CDLADVANCEBLOCK - Advance Block
    df['CDLADVANCEBLOCK'] = ta.CDLADVANCEBLOCK(df['open'], df['high'],df['low'],df['close'])
    #CDLBELTHOLD - Belt-hold
    df['CDLBELTHOLD'] = ta.CDLBELTHOLD(df['open'], df['high'],df['low'],df['close'])
    #CDLBREAKAWAY - Breakaway
    df['CDLBREAKAWAY'] = ta.CDLBREAKAWAY(df['open'], df['high'],df['low'],df['close'])
    #CDLCLOSINGMARUBOZU - Closing Marubozu
    df['CDLCLOSINGMARUBOZU'] = ta.CDLCLOSINGMARUBOZU(df['open'], df['high'],df['low'],df['close'])
    #CDLCONCEALBABYSWALL - Concealing Baby Swallow
    df['integer'] = ta.CDLCONCEALBABYSWALL(df['open'], df['high'],df['low'],df['close'])
    #CDLCOUNTERATTACK - Counterattack
    df['CDLCOUNTERATTACK'] = ta.CDLCOUNTERATTACK(df['open'], df['high'],df['low'],df['close'])
    #CDLDARKCLOUDCOVER - Dark Cloud Cover
    df['CDLDARKCLOUDCOVER'] = ta.CDLDARKCLOUDCOVER(df['open'], df['high'],df['low'],df['close'], penetration=0)
    #CDLDOJI - Doji
    df['CDLDOJI'] = ta.CDLDOJI(df['open'], df['high'],df['low'],df['close'])
    #CDLDOJISTAR - Doji Star
    df['CDLDOJISTAR'] = ta.CDLDOJISTAR(df['open'], df['high'],df['low'],df['close'])
    #CDLDRAGONFLYDOJI - Dragonfly Doji
    df['CDLDRAGONFLYDOJI'] = ta.CDLDRAGONFLYDOJI(df['open'], df['high'],df['low'],df['close'])
    #CDLENGULFING - Engulfing Pattern
    df['CDLENGULFING'] = ta.CDLENGULFING(df['open'], df['high'],df['low'],df['close'])
    #CDLEVENINGDOJISTAR - Evening Doji Star
    df['CDLEVENINGDOJISTAR'] = ta.CDLEVENINGDOJISTAR(df['open'], df['high'],df['low'],df['close'], penetration=0)
    #CDLEVENINGSTAR - Evening Star
    df['CDLEVENINGSTAR'] = ta.CDLEVENINGSTAR(df['open'], df['high'],df['low'],df['close'], penetration=0)
    #CDLGAPSIDESIDEWHITE - Up/Down-gap side-by-side white lines
    df['CDLGAPSIDESIDEWHITE']= ta.CDLGAPSIDESIDEWHITE(df['open'], df['high'],df['low'],df['close'])
    #CDLGRAVESTONEDOJI - Gravestone Doji
    df['CDLGRAVESTONEDOJI'] = ta.CDLGRAVESTONEDOJI(df['open'], df['high'],df['low'],df['close'])
    #CDLHAMMER - Hammer
    df['CDLHAMMER'] = ta.CDLHAMMER(df['open'], df['high'],df['low'],df['close'])
    #CDLHANGINGMAN - Hanging Man
    df['CDLHANGINGMAN'] = ta.CDLHANGINGMAN(df['open'], df['high'],df['low'],df['close'])
    #CDLHARAMI - Harami Pattern
    df['CDLHARAMI'] = ta.CDLHARAMI(df['open'], df['high'],df['low'],df['close'])
    #CDLHARAMICROSS - Harami Cross Pattern
    df['CDLHARAMICROSS'] = ta.CDLHARAMICROSS(df['open'], df['high'],df['low'],df['close'])
    #CDLHIGHWAVE - High-Wave Candle
    df['CDLHIGHWAVE'] = ta.CDLHIGHWAVE(df['open'], df['high'],df['low'],df['close'])
    #CDLHIKKAKE - Hikkake Pattern
    df['CDLHIKKAKE'] = ta.CDLHIKKAKE(df['open'], df['high'],df['low'],df['close'])
    #CDLHIKKAKEMOD - Modified Hikkake Pattern
    df['CDLHIKKAKEMOD'] = ta.CDLHIKKAKEMOD(df['open'], df['high'],df['low'],df['close'])
    #CDLHOMINGPIGEON - Homing Pigeon
    df['CDLHOMINGPIGEON'] = ta.CDLHOMINGPIGEON(df['open'], df['high'],df['low'],df['close'])
    #CDLIDENTICAL3CROWS - Identical Three Crows
    df['CDLIDENTICAL3CROWS'] = ta.CDLIDENTICAL3CROWS(df['open'], df['high'],df['low'],df['close'])
    #CDLINNECK - In-Neck Pattern
    df['CDLINNECK'] = ta.CDLINNECK(df['open'], df['high'],df['low'],df['close'])
    #CDLINVERTEDHAMMER - Inverted Hammer
    df['CDLINVERTEDHAMMER'] = ta.CDLINVERTEDHAMMER(df['open'], df['high'],df['low'],df['close'])
    #CDLKICKING - Kicking
    df['CDLKICKING'] = ta.CDLKICKING(df['open'], df['high'],df['low'],df['close'])
    #CDLKICKINGBYLENGTH - Kicking - bull/bear determined by the longer marubozu
    df['CDLKICKINGBYLENGTH'] = ta.CDLKICKINGBYLENGTH(df['open'], df['high'],df['low'],df['close'])
    #CDLLADDERBOTTOM - Ladder Bottom
    df['CDLLADDERBOTTOM'] = ta.CDLLADDERBOTTOM(df['open'], df['high'],df['low'],df['close'])
    #CDLLONGLEGGEDDOJI - Long Legged Doji
    df['CDLLONGLEGGEDDOJI'] = ta.CDLLONGLEGGEDDOJI(df['open'], df['high'],df['low'],df['close'])
    #CDLLONGLINE - Long Line Candle
    df['CDLLONGLINE'] = ta.CDLLONGLINE(df['open'], df['high'],df['low'],df['close'])
    #CDLMARUBOZU - Marubozu
    df['CDLMARUBOZU'] = ta.CDLMARUBOZU(df['open'], df['high'],df['low'],df['close'])
    #CDLMATCHINGLOW - Matching Low
    df['CDLMATCHINGLOW'] = ta.CDLMATCHINGLOW(df['open'], df['high'],df['low'],df['close'])
    #CDLMATHOLD - Mat Hold
    df['CDLMATHOLD'] = ta.CDLMATHOLD(df['open'], df['high'],df['low'],df['close'], penetration=0)
    #CDLMORNINGDOJISTAR - Morning Doji Star
    df['CDLMORNINGDOJISTAR'] = ta.CDLMORNINGDOJISTAR(df['open'], df['high'],df['low'],df['close'], penetration=0)
    #CDLMORNINGSTAR - Morning Star
    df['CDLMORNINGSTAR'] = ta.CDLMORNINGSTAR(df['open'], df['high'],df['low'],df['close'], penetration=0)
    #CDLONNECK - On-Neck Pattern
    df['CDLONNECK'] = ta.CDLONNECK(df['open'], df['high'],df['low'],df['close'])
    #CDLPIERCING - Piercing Pattern
    df['CDLPIERCING'] = ta.CDLPIERCING(df['open'], df['high'],df['low'],df['close'])
    #CDLRICKSHAWMAN - Rickshaw Man
    df['CDLRICKSHAWMAN'] = ta.CDLRICKSHAWMAN(df['open'], df['high'],df['low'],df['close'])
    #CDLRISEFALL3METHODS - Rising/Falling Three Methods
    df['CDLRISEFALL3METHODS'] = ta.CDLRISEFALL3METHODS(df['open'], df['high'],df['low'],df['close'])
    #CDLSEPARATINGLINES - Separating Lines
    df['CDLSEPARATINGLINES'] = ta.CDLSEPARATINGLINES(df['open'], df['high'],df['low'],df['close'])
    #CDLSHOOTINGSTAR - Shooting Star
    df['CDLSHOOTINGSTAR'] = ta.CDLSHOOTINGSTAR(df['open'], df['high'],df['low'],df['close'])
    #CDLSHORTLINE - Short Line Candle
    df['CDLSHORTLINE'] = ta.CDLSHORTLINE(df['open'], df['high'],df['low'],df['close'])
    #CDLSPINNINGTOP - Spinning Top
    df['CDLSPINNINGTOP'] = ta.CDLSPINNINGTOP(df['open'], df['high'],df['low'],df['close'])
    #CDLSTALLEDPATTERN - Stalled Pattern
    df['CDLSTALLEDPATTERN'] = ta.CDLSTALLEDPATTERN(df['open'], df['high'],df['low'],df['close'])
    #CDLSTICKSANDWICH - Stick Sandwich
    df['CDLSTICKSANDWICH'] = ta.CDLSTICKSANDWICH(df['open'], df['high'],df['low'],df['close'])
    #CDLTAKURI - Takuri (Dragonfly Doji with very long lower shadow)
    df['CDLTAKURI'] = ta.CDLTAKURI(df['open'], df['high'],df['low'],df['close'])
    #CDLTASUKIGAP - Tasuki Gap
    df['CDLTASUKIGAP'] = ta.CDLTASUKIGAP(df['open'], df['high'],df['low'],df['close'])
    #CDLTHRUSTING - Thrusting Pattern
    df['CDLTHRUSTING'] = ta.CDLTHRUSTING(df['open'], df['high'],df['low'],df['close'])
    #CDLTRISTAR - Tristar Pattern
    df['CDLTRISTAR'] = ta.CDLTRISTAR(df['open'], df['high'],df['low'],df['close'])
    #CDLUNIQUE3RIVER - Unique 3 River
    df['CDLUNIQUE3RIVER'] = ta.CDLUNIQUE3RIVER(df['open'], df['high'],df['low'],df['close'])
    #CDLUPSIDEGAP2CROWS - Upside Gap Two Crows
    df['CDLUPSIDEGAP2CROWS'] = ta.CDLUPSIDEGAP2CROWS(df['open'], df['high'],df['low'],df['close'])
    # CDLXSIDEGAP3METHODS - Upside/Downside Gap Three Methods
    df['CDLXSIDEGAP3METHODS'] = ta.CDLXSIDEGAP3METHODS(df['open'], df['high'],df['low'],df['close'])

    df['close_ch'] =df['close'].shift(-1) - df['close']
    def load_bp(val):
        if val > 0:
            return 1
        elif val < 0:
            return -1
        else:
            return 0
    df['close_ch'] = df['close_ch'].map(load_bp)

    # 数据标准化处理
    # def data_handing(val):
    #     if val > 0:
    #         return val/100
    #     elif val < 0:
    #         return val/100
    #     else:
    #         return 0
    # columns = df.iloc[:,5:].columns.tolist()
    # for c in columns:
    #     df[c] = df[c].map(data_handing)



    # # 2.提取特征变量和目标变量

    x = df.drop(columns=['close','open','high','low','volume','close_ch'])
    y = df['close_ch']    

    # # 3.划分训练集和测试集
    from sklearn.model_selection import train_test_split
    X_train, X_test, y_train, y_test = train_test_split(x.values,y, test_size=0.2, random_state=123)

    # # 4.模型训练及搭建
    from xgboost import XGBClassifier
    clf = XGBClassifier(n_estimators=100, learning_rate=0.05)
    clf.fit(X_train, y_train)

    # **10.2.3 模型预测及评估**
    y_pred = clf.predict(X_test)
    a = pd.DataFrame()  # 创建一个空DataFrame
    a['预测值'] = list(y_pred)
    a['实际值'] = list(y_test)
    # a.head()

    from sklearn.metrics import accuracy_score
    score = accuracy_score(y_pred, y_test)
    obj_data['score'] = score
    # print(score)

    y_pred_proba = clf.predict_proba(X_test)
    obj_data['y_pred_proba'] = y_pred_proba[0:5]
    # print(y_pred_proba[0:5])  # 查看前5个预测的概率

    features = x.columns  # 获取特征名称
    importances = clf.feature_importances_  # 获取特征重要性

    importances_df = pd.DataFrame()
    importances_df['特征名称'] = features
    importances_df['特征重要性'] = importances

    importances_sort_df = importances_df.sort_values('特征重要性', ascending=False)
    obj_data['importances_sort_df'] = importances_sort_df[:20]
    # print(importances_sort_df[:20])

    csv_arr.append(obj_data)

if __name__ == '__main__':
    startDate = '2021-01-01'  #开始时间
    endDate = '2021-08-31'   #结束时间
    codeArr = ['AG', 'A', 'AL', 'AP', 'AU', 'BB', 'BC', 'B', 'BU', 'CF', 'CJ', 'C', 'CS', 'CU', 'CY', 'EB', 'EG', 'FB', 'FG', 'FU', 'HC', 'IH', 'I', 'JD', 'J', 'JM', 'JR', 'LH', 'L', 'LU', 'MA', 'M', 'NI', 'NR', 'OI', 'PB', 'PF', 'PG', 'PK', 'P', 'PM', 'PP', 'RB', 'RI', 'RR', 'RS', 'RU', 'SA', 'SC', 'SF', 'SM', 'SN', 'SP', 'SR', 'SS', 'TA', 'TF', 'T', 'TS', 'UR', 'V', 'WH', 'WR', 'Y', 'ZN']   #品种数组
    codeArr = ['TS']
    for item in tqdm(codeArr):
        test = run_all(item,startDate,endDate)

    # df = pd.DataFrame(csv_arr)
    # df.to_csv('importances_sort_df.csv',sep=',',index=True,header=True)


